package com.xhx.api.controller.animals;


import com.xhx.model.animals.Dog;
import com.xhx.model.utils.PageResult;
import com.xhx.model.utils.PagingModel;
import com.xhx.service.animals.DogService;
import com.xhx.utils.JsonResult;
import com.xhx.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("dog")
public class DogController {

    @Autowired
    @Qualifier("dogServiceImpl")
    DogService dogService;

    @Autowired
    RedisUtils redisUtils;

    @PostMapping()
    public JsonResult addDog(@RequestBody Dog dog) {
        int i = dogService.addDog(dog);
        if (i==1){
            System.out.println(dog);
            return new JsonResult(200,"",dog);
        }else {
            System.out.println(dog);
            return new JsonResult(400,"",dog);
        }
    }
    @PutMapping("{id}")
    public JsonResult udpDog(@RequestBody Dog dog) {
        int i = dogService.udpDog(dog);
        if (i==1){
            System.out.println(dog);
            return new JsonResult(200,"",dog);
        }else {
            System.out.println(dog);
            return new JsonResult(400,"",dog);
        }
    }
    @GetMapping("{id}")
    public JsonResult findDog(@PathVariable(name = "id") int id) {
        Map dog = dogService.findDog(id);
        System.out.println(dog);
        return new JsonResult(200,"",dog);
    }
    @GetMapping("/a/{id}")
    public JsonResult findADog(@PathVariable(name = "id") int id) {
        Map dog = dogService.findADog(id);
        System.out.println(dog);
        return new JsonResult(200,"",dog);
    }

    @DeleteMapping("{id}")
    public JsonResult delDog(@PathVariable(name = "id") int id){
        int i = dogService.delDog(id);
        if (i==1){
            return new JsonResult(200,"",i);
        }else {
            return new JsonResult(400,"",i);
        }
    }

    @GetMapping("list")
    public JsonResult findDogs(  @RequestParam(name = "pid", defaultValue = "0") int pid,
                                 @RequestParam(name = "page", defaultValue = "1") int page,
                                 @RequestParam(name = "size", defaultValue = "10") int size){

        // 分页参数
        page = page < 1 ? 1 : page;
        size = size < 1 ? 1 : size;

        PagingModel pagingModel = new PagingModel();
        pagingModel.page = page;
        pagingModel.size = (size > 1000 ? 1000 : size);
        // 当前页数据
        Dog filterModel = new Dog();
        List<Dog> dataModels = this.dogService.findDogs(pagingModel, filterModel);
        Object data = dataModels;
        // 组装响应数据
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(0); // 状态码
        jsonResult.setMsg("ok"); // 消息
        jsonResult.setData(new PageResult(pagingModel.count, pagingModel.page, pagingModel.size, data, null)); // 业务数据

        // 输出响应结果
        return jsonResult;
    }
    @PostMapping("list")
    public JsonResult findDogsPost(  @RequestParam(name = "pid", defaultValue = "0") int pid,
                                 @RequestParam(name = "page", defaultValue = "1") int page,
                                 @RequestParam(name = "size", defaultValue = "10") int size,
                                 @RequestBody Dog filterModel){

        // 分页参数
        page = page < 1 ? 1 : page;
        size = size < 1 ? 1 : size;

        PagingModel pagingModel = new PagingModel();
        pagingModel.page = page;
        pagingModel.size = (size > 1000 ? 1000 : size);
        // 当前页数据
        List<Dog> dataModels = this.dogService.findDogs(pagingModel, filterModel);
        Object data = dataModels;
        // 组装响应数据
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(0); // 状态码
        jsonResult.setMsg("ok"); // 消息
        jsonResult.setData(new PageResult(pagingModel.count, pagingModel.page, pagingModel.size, data, null)); // 业务数据

        // 输出响应结果
        return jsonResult;
    }

    @PostMapping("redisadd")
    public JsonResult redisadd(@RequestParam(name = "key") String key,
                               @RequestParam(name = "value") String value){
        redisUtils.setRedis(key,value);
        return new JsonResult(200,"",null);
    }
    @GetMapping("redisget")
    public JsonResult redisget(@RequestParam(name = "key") String key){
        Object redis = redisUtils.getRedis(key);
        return new JsonResult(200,"",redis);
    }


}
